package com.yunhe.log.controller.data;

import com.yunhe.common.model.PageParam;
import com.yunhe.log.domain.data.SystemMonitorLog;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.DomainList;
import com.yunhe.common.model.ResultObject;
import com.yunhe.log.service.data.SystemMonitorLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Time;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 系统日志记录Controller
 * @desc 系统日志记录控制类定义
 * @author liuronglei
 */
@Api(value = "systemMonitorLog",description = "系统日志记录维护", tags = {"Data_SystemMonitorLog"})
@RestController
@RequestMapping("/systemMonitorLogs")
public class SystemMonitorLogController {

    @Autowired
    private SystemMonitorLogService systemMonitorLogService;

    /**
     * 新增系统日志记录
     * @param systemMonitorLog 系统日志记录对象
     */
    @ApiOperation("新增系统日志记录")
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ResultObject> post(@RequestBody SystemMonitorLog systemMonitorLog)
            throws ArgumentErrorException, UniqueConstraintsException {
        SystemMonitorLog newSystemMonitorLog = systemMonitorLogService.createSystemMonitorLog(systemMonitorLog);
        return new ResponseEntity<>(new ResultObject<>(newSystemMonitorLog), HttpStatus.OK);
    }

    @ApiOperation("新增导出操作记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menu", value = "功能菜单名称", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public ResponseEntity<ResultObject> createExportRecord(@RequestParam("menu") String menu,
                                                           @RequestParam("stationId") Long stationId,
                                                           @RequestParam("userId") Long userId) {
        SystemMonitorLog result = systemMonitorLogService.createExportRecord(stationId, menu, userId);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    /**
     * 批量新增系统日志记录
     * @param domainList 系统日志记录对象列表
     */
    @ApiOperation("批量新增系统日志记录")
    @RequestMapping(value = "/batch", method = RequestMethod.POST)
    public ResponseEntity<ResultObject> batchPost(@RequestBody DomainList<SystemMonitorLog> domainList)
            throws ArgumentErrorException, UniqueConstraintsException {
        List<SystemMonitorLog> systemMonitorLogList = domainList.getList();
        Iterable<SystemMonitorLog> newSystemMonitorLogs = systemMonitorLogService.createSystemMonitorLog(systemMonitorLogList);
        return new ResponseEntity<>(new ResultObject<>(newSystemMonitorLogs), HttpStatus.OK);
    }

    /**
     * 更新系统日志记录
     */
    @ApiOperation("更新系统日志记录(修改指定字段)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "date", value = "日期，如“2018-11-11”", required = true, dataType = "date", paramType = "query"),
            @ApiImplicitParam(name = "time", value = "时间，如“00:00:00”", required = true, dataType = "date", paramType = "query"),
            @ApiImplicitParam(name = "typeCode", value = "类型代码", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "内容", required = true, dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
    public ResponseEntity<ResultObject> updateSystemMonitorLog(@PathVariable("id") Long id,
                                                               @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Optional<Date> date,
                                                               @RequestParam("time") @DateTimeFormat(pattern = "HH:mm:ss") Optional<Time> time,
                                                               @RequestParam("typeId") Optional<Long> typeCode,
                                                               @RequestParam("type") Optional<String> type,
                                                               @RequestParam("content") Optional<String> content)
            throws ObjectNotFoundException, UniqueConstraintsException, ArgumentErrorException {
        SystemMonitorLog result = systemMonitorLogService.updateSystemMonitorLog(id, date.orElse(null),
                time.orElse(null), typeCode.orElse(null), type.orElse(null), content.orElse(null));
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("更新系统日志记录(修改指定字段)")
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<ResultObject> updateSystemMonitorLog(@RequestBody SystemMonitorLog systemMonitorLog)
            throws ObjectNotFoundException, UniqueConstraintsException, ArgumentErrorException {
        SystemMonitorLog result = systemMonitorLogService.updateSystemMonitorLog(systemMonitorLog);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    /**
     * 根据条件获得系统日志记录列表
     */
    @ApiOperation("根据条件获得系统日志记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dtime", value = "日期", required = true, dataType = "date", paramType = "query"),
            @ApiImplicitParam(name = "time", value = "时间", dataType = "date", paramType = "query"),
            @ApiImplicitParam(name = "typeId", value = "类型Id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "typeTitle", value = "类型", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "内容", dataType = "string", paramType = "query")
    })
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<ResultObject> search(@RequestParam("dtime") @DateTimeFormat(pattern = "yyyy-MM-dd") Optional<List<Date>> dtime,
                                               @RequestParam("time") @DateTimeFormat(pattern = "HH:mm:ss") Optional<Time> time,
                                               @RequestParam("typeId") Optional<List<Long>> typeId,
                                               @RequestParam("typeTitle") Optional<String> typeTitle,
                                               @RequestParam("content") Optional<String> content,
                                               @ModelAttribute PageParam pageParam) {
        Iterable<SystemMonitorLog> results = systemMonitorLogService.findSystemMonitorLogs(dtime.orElse(null),
                time.orElse(null), typeId.orElse(null), typeTitle.orElse(null),
                content.orElse(null), pageParam);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }
}
